﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Storm.TextEditor.Editor.CodeCompletion
{
	/// <summary>
	/// Represents a generic ListBox with icons.
	/// </summary>
    [ToolboxItem(false)]
	public class CodeCompletionListBox
		: ListBox
	{
		#region Fields

		private ICompletionDataProvider completionDataProvider = null;

		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets the current ICompletionDataProvider of the CodeCompletionListBox.
		/// </summary>
		internal ICompletionDataProvider CompletionDataProvider
		{
			get { return completionDataProvider; }
			set
			{
				completionDataProvider = value;
				this.Invalidate();
			}
		}

		/// <summary>
		/// Gets or sets the height of an item in the <see cref="T:System.Windows.Forms.ListBox"/>.
		/// </summary>
		/// <value></value>
		/// <returns>
		/// The height, in pixels, of an item in the control.
		/// </returns>
		/// <exception cref="T:System.ArgumentOutOfRangeException">
		/// The <see cref="P:System.Windows.Forms.ListBox.ItemHeight"/> property was set to less than 0 or more than 255 pixels.
		/// </exception>
		/// <PermissionSet>
		/// 	<IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
		/// 	<IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
		/// 	<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/>
		/// 	<IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
		/// </PermissionSet>
		public override int ItemHeight
		{
			get { return Math.Max(this.CompletionDataProvider.ImageList.ImageSize.Height, (int)(this.Font.Height)); }
			set { }
		}

		#endregion

		#region Methods

		#region Protected

		/// <summary>
		/// Raises the <see cref="E:System.Windows.Forms.ListBox.DrawItem"/> event.
		/// </summary>
		/// <param name="e">A <see cref="T:System.Windows.Forms.DrawItemEventArgs"/> that contains the event data.</param>
		protected override void OnDrawItem(DrawItemEventArgs e)
		{
			e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

			e.DrawBackground();
			e.DrawFocusRectangle();

			ICompletionData completionData = null;
			Rectangle bounds = e.Bounds;
			Size imageSize = this.CompletionDataProvider.ImageList.ImageSize;

			try
			{
				completionData = (ICompletionData)this.Items[e.Index];
				if (completionData.ImageIndex > -1)
				{
					this.CompletionDataProvider.ImageList.Draw(e.Graphics, bounds.Left, bounds.Top, completionData.ImageIndex);
					e.Graphics.DrawString(completionData.Text, e.Font, new SolidBrush(e.ForeColor), bounds.Left + imageSize.Width, bounds.Top);
				}
				else
					e.Graphics.DrawString(completionData.Text, e.Font, new SolidBrush(e.ForeColor), bounds.Left, bounds.Top);
			}
			catch
			{
				if (e.Index != -1)
					e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), bounds.Left, bounds.Top);
				else
					e.Graphics.DrawString(this.Text, e.Font, new SolidBrush(e.ForeColor), bounds.Left, bounds.Top);
			}
		}

		#endregion

		#endregion

		/// <summary>
		/// Initializes a new instance of the <see cref="CodeCompletionListBox"/> class.
		/// </summary>
		public CodeCompletionListBox()
		{
			this.DrawMode = DrawMode.OwnerDrawFixed;
		}
	}
}
